/**
 *	TrueMusic - simple Java music player
 *
 *	TrueMusic is free software; you can redistribute it
 *	and/or modify it under the terms of the GNU General
 *	Public License as published by the Free Software
 *	Foundation; either version 2 of the License or (at
 *	your option) any later version.
 *
 *	This program is distributed in the hope that it will be
 *	useful but WITHOUT ANY WARRANTY; without even the
 *	implied warranty of the MERCHANTABILITY or FITNESS FOR
 *	A PARTICULAR PURPOSE. See the GNU General Public
 *	License for more details.
 *
 *	Copyright (c)2005-2007 Ryan Macnak <rmacnak@gmail.com>
 */

package com.servequake.truemusic;

import java.applet.*;
import java.io.*;

/**
 * Audio implemented with java.applet.AudioClip, WARNING: Does not completely implement all functions expected from an Audio.
 * AudioSystem maybe provides better functionally: possible future migration to allow seeking,volume?
 */
public class AppletClipAudio implements Audio
{
	AudioClip a;
	boolean playing = false;

	String res;
	PlaybackListener l;
		
	public void load(String res, PlaybackListener l) throws Exception
	{
		this.res = res;
		this.l = l;

		a = Applet.newAudioClip(new File(res).toURL());
		a = null;
	}

	public void play()
	{
		if(a==null) try{ a = Applet.newAudioClip(new File(res).toURL()); }catch(Exception e){}

		new Thread()
		{
			public void run()
			{
				playing = true;
				a.play();
				playing = false;
				l.playbackCompleted();
			}
		}.start();
	}

	public void pause()
	{
		stop();
	}

	public void stop()
	{
		new Thread()
		{
			public void run()
			{
				a.stop();
				playing = false;
			}
		}.start();
	}


	public int getFrames()
	{
		return 0;
	}

	public void seek(int frame) {} //ignore

	
	public boolean isPlaying(){return playing;}

	public String getResource(){return res;}
	public String getTitle(){return new File(res).getName();}
	public String getArtist(){return "";}
	public String getAlbum(){return "";}
	public String getTrack(){return "";}
	public String getLyrics(){return "";}
	public String getGenre(){return "";}
	public String toString(){return new File(res).getName();}

	public String formatName(){return "Aiff/Au";}
	public String[] getExtensions() {return new String[]{".au",".aif",".aiff"};}
}
